package net.zjitc.controller;

import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Role;
import net.zjitc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class RolesController {
    @Autowired
    private RoleService roleService;

    /**
     * 角色列表
     * @return
     */
    @GetMapping("roles")
    public ResponseResult getroles(){
        ResponseResult result=new ResponseResult();
        List<Role> roles = roleService.getRole();
        result.setSuccess("获取成功",roles);
        return result;
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("roles")
    public ResponseResult addRole(@RequestBody Role role){
        ResponseResult result=new ResponseResult();
        Role role1= roleService.addRole(role);
        result.setCreateSuccess("创建成功",role);
        return result;
    }
    /**
     * 根据id查询角色
     */
    @GetMapping("roles/{id}")
    public ResponseResult findRoleById(@PathVariable("id") int id){
        ResponseResult result =new ResponseResult();
        Role role=roleService.findRoleById(id);
        if (role!=null){
            result.setSuccess("获取成功",role);
        }else {
            result.setError("获取失败");
        }
        return result;
    }

    /**
     * 编辑提交角色
     * @param role
     * @param id
     * @return
     */
    @PutMapping("roles/{id}")
    public ResponseResult updateRole(@RequestBody Role role,@PathVariable("id") int id){
        ResponseResult result=new ResponseResult();
        Role role1= roleService.updateRole(role,id);
        result.setSuccess("获取成功",role1);
        return result;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("roles/{id}")
    public ResponseResult deleteRole(@PathVariable("id") int id){
        ResponseResult result=new ResponseResult();
        int i= roleService.delete(id);
        result.setSuccess("删除成功");
        return  result;
    }

    /**
     * 角色授权
     * @param id
     * @param rids
     * @return
     */
    @PostMapping("roles/{id}/rights")
    public ResponseResult addrids(@PathVariable("id") int id,@RequestParam String rids){
        ResponseResult result=new ResponseResult();
        int i= roleService.addrids(id,rids);
        result.setSuccess("更新成功");
        return  result;
    }

    /**
     * 删除角色授权指定权限
     * @param id
     * @param rightid
     * @return
     */
    @DeleteMapping("roles/{id}/right/{rightid}")
    public ResponseResult deleteRightId(@PathVariable("id") int id,@PathVariable("rightid") int rightid){
        ResponseResult result=new ResponseResult();
        Role role= roleService.deleteRightId(id,rightid);
        result.setSuccess("取消权限成功",role);
        return result;
    }
}
